using System;
using System.Text.Json.Serialization;
using System.Collections.Generic;

namespace KeyAuthService.Models
{
    /// <summary>
    /// 自动会话令牌模型
    /// </summary>
    public class SessionToken
    {
        /// <summary>
        /// 令牌ID
        /// </summary>
        [JsonPropertyName("jti")]
        public string TokenId { get; set; } = Guid.NewGuid().ToString("N");
        
        /// <summary>
        /// 发行者（应用ID）
        /// </summary>
        [JsonPropertyName("iss")]
        public string Issuer { get; set; } = string.Empty;
        
        /// <summary>
        /// 主题（用户身份）
        /// </summary>
        [JsonPropertyName("sub")]
        public string Subject { get; set; } = string.Empty;
        
        /// <summary>
        /// 接收者
        /// </summary>
        [JsonPropertyName("aud")]
        public string Audience { get; set; } = "keyauth-service";
        
        /// <summary>
        /// 发行时间
        /// </summary>
        [JsonPropertyName("iat")]
        public long IssuedAt { get; set; } = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
        
        /// <summary>
        /// 生效时间
        /// </summary>
        [JsonPropertyName("nbf")]
        public long NotBefore { get; set; } = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
        
        /// <summary>
        /// 过期时间
        /// </summary>
        [JsonPropertyName("exp")]
        public long ExpiresAt { get; set; }
        
        /// <summary>
        /// 密钥ID
        /// </summary>
        [JsonPropertyName("key_id")]
        public string KeyId { get; set; } = string.Empty;
        
        /// <summary>
        /// 公钥地址
        /// </summary>
        [JsonPropertyName("key_addr")]
        public string KeyAddress { get; set; } = string.Empty;
        
        /// <summary>
        /// 会话信息
        /// </summary>
        [JsonPropertyName("session_info")]
        public SessionInfo SessionInfo { get; set; } = new SessionInfo();
        
        /// <summary>
        /// 设置过期时间（分钟）
        /// </summary>
        public void SetExpiry(int minutes)
        {
            ExpiresAt = DateTimeOffset.UtcNow.AddMinutes(minutes).ToUnixTimeSeconds();
        }
    }

    /// <summary>
    /// 会话信息
    /// </summary>
    public class SessionInfo
    {
        /// <summary>
        /// 连接ID
        /// </summary>
        [JsonPropertyName("conn_id")]
        public string ConnectionId { get; set; } = string.Empty;
        
        /// <summary>
        /// 权限作用域
        /// </summary>
        [JsonPropertyName("scopes")]
        public List<string> Scopes { get; set; } = new List<string>();
        
        /// <summary>
        /// 客户端信息
        /// </summary>
        [JsonPropertyName("client_info")]
        public Dictionary<string, string> ClientInfo { get; set; } = new Dictionary<string, string>();
    }
} 